/**   
 * Copyright © 2018 HIT akeil. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com.akeil.ui.dialogs 
 * @author: snow   
 * @date: 2018年6月27日 上午8:19:59 
 */
package cn.edu.hit.education.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.edu.hit.education.model.AuthenticationInformation;
import cn.edu.hit.education.pojo.Role;
import cn.edu.hit.education.pojo.User;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 * Copyright: Copyright (c) 2018 HIT akeil
 * 
 * @ClassName: LicenseDialog.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: snow
 * @date: 2018年6月27日 上午8:19:59
 *
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2018年6月27日
 *        snow v1.0.0 修改原因
 */

public class LoginDialog extends TitleAreaDialog {
	private Text textPassword;
	private Text textUsername;
	private Combo comboRole;
	private AuthenticationInformation authenticationInformation;
	
	public LoginDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub

	}

	@Override
	protected void configureShell(Shell newShell) {
		// TODO 自动生成的方法存根
		super.configureShell(newShell);
		newShell.setText("登录系统");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = (Composite) super.createDialogArea(parent);
		// 自定义要创建的内容

		createContentPane(composite);
		return composite;
	}

	private void createContentPane(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		// gd_composite.widthHint = 174;
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1,	1);
		gd_composite.heightHint = 193;
		composite.setLayoutData(gd_composite);

		composite.setLayout(new FormLayout());

		final Label label_name = new Label(composite, SWT.NONE);
		final FormData formData = new FormData();
		formData.left = new FormAttachment(0, 46);
		label_name.setLayoutData(formData);
		label_name.setText("系统用户");

		textUsername = new Text(composite, SWT.BORDER);
		formData.top = new FormAttachment(textUsername, 4, SWT.TOP);
		formData.right = new FormAttachment(textUsername, -4);
		textUsername.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				User user = authenticationInformation.getCurrentUser();
				user.setUsername(textUsername.getText().trim());
			}
		});
		final FormData fd_textUsername = new FormData();
		fd_textUsername.bottom = new FormAttachment(0, 35);
		fd_textUsername.left = new FormAttachment(0, 100);
		fd_textUsername.right = new FormAttachment(100, -10);
		fd_textUsername.top = new FormAttachment(0, 10);
		textUsername.setLayoutData(fd_textUsername);
		textUsername.setText(authenticationInformation.getCurrentUser().getUsername());

		textPassword = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		textPassword.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				User user = authenticationInformation.getCurrentUser();
				user.setPassword(textPassword.getText().trim());
			}
		});
		final FormData fd_textPassword = new FormData();
		fd_textPassword.top = new FormAttachment(textUsername, 6);
		fd_textPassword.right = new FormAttachment(100, -10);
		textPassword.setLayoutData(fd_textPassword);
		textPassword.setText(authenticationInformation.getCurrentUser().getPassword());
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("用户密码");
		fd_textPassword.left = new FormAttachment(label, 6);
		FormData fd_label = new FormData();
		fd_label.left = new FormAttachment(0, 46);
		fd_label.right = new FormAttachment(100, -260);
		fd_label.top = new FormAttachment(textPassword, 4, SWT.TOP);
		label.setLayoutData(fd_label);
		
		Label label_1 = new Label(composite, SWT.NONE);
		FormData fd_label_1 = new FormData();
		fd_label_1.left = new FormAttachment(0, 46);
		label_1.setLayoutData(fd_label_1);
		label_1.setText("用户角色");
		
		comboRole = new Combo(composite, SWT.NONE);
		comboRole.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				authenticationInformation.setCurrentRoleSelection(comboRole.getSelectionIndex());
			}
		});
		fd_textPassword.bottom = new FormAttachment(comboRole, -6);
		fd_label_1.top = new FormAttachment(comboRole, 4, SWT.TOP);
		fd_label_1.right = new FormAttachment(comboRole, -6);
		FormData fd_comboRole = new FormData();
		fd_comboRole.left = new FormAttachment(0, 100);
		fd_comboRole.right = new FormAttachment(100, -10);
		fd_comboRole.bottom = new FormAttachment(100, -48);
		fd_comboRole.top = new FormAttachment(0, 71);
		comboRole.setLayoutData(fd_comboRole);
		List<String> names = initData();
		comboRole.setItems(names.toArray(new String[names.size()]));
		comboRole.select(authenticationInformation.getCurrentRoleSelection());
		
		this.setTitle("请输入系统用户名和密码");
		this.setMessage("系统用户名和密码", IMessageProvider.INFORMATION);
	}
	
	public AuthenticationInformation getAuthenticationInformation() {
		return authenticationInformation;
	}

	public void setAuthenticationInformation(
			AuthenticationInformation authenticationInformation) {
		this.authenticationInformation = authenticationInformation;
	}

	@Override
	protected Point getInitialSize() {
		// TODO 自动生成的方法存根
		return new Point(360, 270);
	}

	private List<String> initData(){
		List<String> roleNames = new ArrayList<String>();
		if(authenticationInformation.getRoles() != null && authenticationInformation.getRoles().size() > 0){
			for (Role role : authenticationInformation.getRoles()) {
				roleNames.add(role.getRole());
			}
		}
		
		return roleNames;
	}
	
	/**
	 * 单击OK进行密码认证
	 */
	/*@Override
	protected void okPressed() {
		// TODO Auto-generated method stub
		try {
			userService.login(textUsername.getText(), textPassword.getText());
			super.okPressed();
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			if (e instanceof UnknownAccountException) {
				System.out.println("用户不存在");
				MessageBox messageBox=new MessageBox(super.getShell(),SWT.OK|SWT.ICON_INFORMATION);
				 messageBox.setText("提示！");
				 messageBox.setMessage("用户名不存在！");
				 messageBox.open();

			} else if (e instanceof IncorrectCredentialsException) {
				System.out.println("密码不正确");
				 MessageBox messageBox=new MessageBox(super.getShell(),SWT.OK|SWT.ICON_INFORMATION);
				 messageBox.setText("提示！");
				 messageBox.setMessage("密码不正确！");
				 messageBox.open();

			} else if (e instanceof LockedAccountException) {
				System.out.println("用户账号被锁定");
				 MessageBox messageBox=new MessageBox(super.getShell(),SWT.OK|SWT.ICON_INFORMATION);
				 messageBox.setText("提示！");
				 messageBox.setMessage("用户账号被锁定！");
				 messageBox.open();

			} else {
				System.out.println("认证失败");
				MessageBox messageBox=new MessageBox(super.getShell(),SWT.OK|SWT.ICON_INFORMATION);
				 messageBox.setText("提示！");
				 messageBox.setMessage("认证失败！");
				 messageBox.open();
			}
		}
	}*/
}
